package com.lc.railway.ui.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.railway.R;
import com.lc.railway.app.AppActivity;
import com.lc.railway.http.request.SelectMerchantInfoApi;
import com.lc.railway.http.response.SelectMerchantInfonNewBean;
import com.lc.railway.ui.AdvertiseLinearSmoothScroller;
import com.lc.railway.ui.adapter.TestColumnnewAdapter;
import com.lc.railway.ui.adapter.TestnewAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class TestNewActivity extends AppActivity {

    private List<SelectMerchantInfonNewBean.MerchantInfoBean.ClassifiesBean> zuoList=new ArrayList<>();
    TestnewAdapter adapter;
    TestColumnnewAdapter columnAdapter;
    LinearLayoutManager plvLayoutManager;
    private RecyclerView plv;
    private RecyclerView column;
    private TextView header_title;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        plv = findViewById(R.id.plv);
        column = findViewById(R.id.column);
        header_title = findViewById(R.id.header_title);

        initSelectMerchantInfo();


    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_test;
    }

    @Override
    protected void initView() {

    }

    private static final String TAG = "TestNewActivity";
    @Override
    protected void initData() {

    }

//
//    用户点击进入商家,查询商家信息
private void initSelectMerchantInfo() {


    EasyHttp.post(this)
            .api(new SelectMerchantInfoApi().setUserid("8").setMerchantid("53")).request(new HttpCallback<SelectMerchantInfonNewBean>(this) {

        @Override
        public void onSucceed(SelectMerchantInfonNewBean data) {
            if (data.getCode()==0){
                zuoList.clear();
                zuoList.addAll(data.getMerchantInfo().getClassifies());
                for (int i = 0; i <data.getMerchantInfo().getClassifies().size() ; i++) {
                    Log.e(TAG, "onSucceed: "+zuoList.get(i).getClassifyName());
                }

                JSONObject jo = null;
                try {
                    jo = new JSONObject(com.alibaba.fastjson.JSONObject.toJSONString(data.getMerchantInfo()));

               ;
                  initColumnData(jo);


                plvLayoutManager = new LinearLayoutManager(TestNewActivity.this){
                    //
                    @Override
                    public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
                        AdvertiseLinearSmoothScroller linearSmoothScroller =
                                new AdvertiseLinearSmoothScroller(recyclerView.getContext());
                        linearSmoothScroller.setTargetPosition(position);
                        startSmoothScroll(linearSmoothScroller);
                    }
                };
                plvLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                plv.setLayoutManager(plvLayoutManager);
                adapter = new TestnewAdapter(TestNewActivity.this);
                adapter.setData( initDataa(jo));
                plv.setAdapter(adapter);


                LinearLayoutManager columnLayoutManager = new LinearLayoutManager(TestNewActivity.this);
                columnLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                column.setLayoutManager(columnLayoutManager);
                columnAdapter = new TestColumnnewAdapter(TestNewActivity.this, new TestColumnnewAdapter.CallBack() {
                    @Override
                    public void onClick(int index) {
                        plv.smoothScrollToPosition(adapter.getItemIndexForGroup(index));
                    }
                });
                columnAdapter.setData(initColumnData(jo));
                column.setAdapter(columnAdapter);


                plv.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                    }

                    @Override
                    public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                        super.onScrolled(recyclerView, dx, dy);
                        int firstVisibleItem = plvLayoutManager.findFirstVisibleItemPosition();
                        Log.e("-----onScrolled-----","firstVisibleItem: " + firstVisibleItem);
                        if (firstVisibleItem >= 0) {
                            header_title.setVisibility(View.VISIBLE);
                            header_title.setText(adapter.getItem(firstVisibleItem).optString("TYPE_NAME"));
                            if (firstVisibleItem == 0 || adapter.isGroup(firstVisibleItem)){
                                columnAdapter.setCurrentIndex(adapter.getGroupIndex(firstVisibleItem));
                            }
                            Log.e("----onScrolled--------","ChildCount: " + recyclerView.getChildCount());
                            if (adapter.isGroup(firstVisibleItem + 1)){
                                View childView = plvLayoutManager.findViewByPosition(firstVisibleItem);;
                                if (childView != null) {
                                    int titleHeight = header_title.getHeight();
                                    int bottom = childView.getBottom();
                                    Log.e("-----onScrolled-------", titleHeight + "   " + bottom);
                                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) header_title.getLayoutParams();
                                    if (bottom <= titleHeight) {
                                        float pushedDistance = bottom - titleHeight;
                                        params.topMargin = (int) pushedDistance;
                                        header_title.setLayoutParams(params);
                                    } else {
                                        if (params.topMargin != 0) {
                                            params.topMargin = 0;
                                            header_title.setLayoutParams(params);
                                        }
                                    }
                                }
                            } else {
                                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) header_title.getLayoutParams();
//                        if (params.topMargin != 0) {
                                params.topMargin = 0;
                                header_title.setLayoutParams(params);
//                        }
                            }

                        } else {
                            header_title.setVisibility(View.INVISIBLE);
                        }
                    }
                });
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }


        }
        @Override
        public void onFail(Exception e) {
            super.onFail(e);

        }
    });




}
    private JSONArray initDataa(JSONObject body) {

        JSONArray list = body.optJSONArray("classifies");
        JSONArray data = new JSONArray();
        for (int i = 0; i < list.length(); i++) {
            JSONObject listJo = list.optJSONObject(i);
            JSONObject typeList = new JSONObject();
            JSONArray goods = listJo.optJSONArray("childrens");
            try {
                typeList.putOpt("_isGroup", true);
                typeList.putOpt("classifyName", listJo.optString("classifyName"));
                data.put(typeList);
                for (int j = 0; j < goods.length(); j++) {
                    JSONObject childrensJo = goods.optJSONObject(j);
                    JSONArray commoditiesJsa = childrensJo.optJSONArray("commodities");
                    if(commoditiesJsa != null){
                        for (int z = 0; z < commoditiesJsa.length(); z++){
                            data.put(commoditiesJsa.optJSONObject(z));
                        }
                    }


                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    private JSONArray initColumnData(JSONObject body) {


        JSONArray list = body.optJSONArray("classifies");
        JSONArray data = new JSONArray();
        for (int i = 0; i < list.length(); i++) {
            JSONObject listJo = list.optJSONObject(i);
            JSONObject typeList = new JSONObject();
            try {
                typeList.putOpt("_isGroup", true);
                typeList.putOpt("classifyName", listJo.optString("classifyName"));
                data.put(typeList);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return data;
    }


}

